/**
 * @(#)ApkConfigController.java 2017年03月1日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.domian.MiguVideoConfigBean;
import org.jsbd.boss.domian.MiguVideoRetentConfigBean;
import org.jsbd.boss.domian.channel.ChannelRetentConfig;
import org.jsbd.boss.service.IMiguVideoConfigService;
import org.jsbd.boss.service.IMiguVideoRetentConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/miguVideoRetentConfig")
public class MiguVideoRetentConfigController{

	private static final Logger LOGGER=LoggerFactory.getLogger(MiguVideoRetentConfigController.class);
	
	@Resource
	private IMiguVideoRetentConfigService miguVideoRetentConfigService;
	@Resource
	private IMiguVideoConfigService miguVideoConfigService;

	/**
	 * 列表页面
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/list")
	public String showMiguVideoConfigList(Model model) throws Exception {
		return "/miguVideoRetentConfig/miguVideoRetentConfigList";
	}
	
	@RequestMapping(value = "/retentList")
	@ResponseBody
	public Map<String, Object> retentPageList(DataGridModel model, MiguVideoRetentConfigBean form) {
		Map<String, Object> map = new HashMap<String, Object>(); 
		List<MiguVideoRetentConfigBean> retrntList = new ArrayList<MiguVideoRetentConfigBean>();
		
		String name = form.getName();
		if (StringUtils.isBlank(name)) {
			map.put("rows", retrntList);
			return map;
		}
		MiguVideoConfigBean queryMvc = new MiguVideoConfigBean();
		queryMvc.setName(name);
		MiguVideoConfigBean miguVideoConfig = miguVideoConfigService.getMiguVideoConfig(queryMvc);
		if (miguVideoConfig == null) {
			map.put("rows", retrntList);
			return map;
		}
		
		retrntList = miguVideoRetentConfigService.getRetentBymarkforPage(miguVideoConfig.getId());
		map.put("rows", retrntList);
		return map;
	}
	
	@RequestMapping(value = "/saveRetent")
	public void saveRetent(@RequestBody MiguVideoRetentConfigBean[] list, HttpServletResponse response) {
		List<MiguVideoRetentConfigBean> mvrcs = Arrays.asList(list);
		
		MiguVideoConfigBean queryMvc = new MiguVideoConfigBean();
		queryMvc.setName(mvrcs.get(0).getName());
		MiguVideoConfigBean miguVideoConfig = miguVideoConfigService.getMiguVideoConfig(queryMvc);
		
		for (MiguVideoRetentConfigBean temp : mvrcs) {
			temp.setMiguVideoId(miguVideoConfig.getId());
		}
		miguVideoRetentConfigService.deleteByMiguVideoConfig(miguVideoConfig.getId());
		miguVideoRetentConfigService.saveRetent(mvrcs);
		ResponseUtils.responseSuccess(response);
	}
	
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	public void deleteRetentBatch(@RequestBody List<Map> list, HttpServletResponse response) {
		String name = (String) list.get(0).get("name");
		MiguVideoConfigBean queryMvc = new MiguVideoConfigBean();
		queryMvc.setName(name);
		MiguVideoConfigBean miguVideoConfig = miguVideoConfigService.getMiguVideoConfig(queryMvc);
		
		for (Map map : list) {
			MiguVideoRetentConfigBean mvrcb = new MiguVideoRetentConfigBean();
			mvrcb.setRetentRate(Integer.valueOf(String.valueOf(map.get("retentRate"))));
			mvrcb.setRetentDay(Integer.valueOf(String.valueOf(map.get("retentDay"))));
			mvrcb.setMiguVideoId(miguVideoConfig.getId());
			miguVideoRetentConfigService.deleteRetentBatch(mvrcb);
		}
		ResponseUtils.responseSuccess(response);
	}
	
}
